package com.dianrui.fmi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.bean.MessageModel;
import com.dianrui.fmi.interfaces.MsgDeleteClick;
import com.dianrui.fmi.util.NoDoubleClickUtils;
import com.dianrui.fmi.view.SwipeMenuLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MessageAdapter extends BaseAdapter {
    private List<MessageModel> list;
    private Context context;
    private MessageClickAdapter messageClickAdapter;
    private MsgDeleteClick msgDeleteClick;

    public MessageAdapter(Context context, List<MessageModel> list,MsgDeleteClick msgDeleteClick) {
        this.list = list;
        this.context = context;
        this.msgDeleteClick=msgDeleteClick;
    }


    public void refresh(List<MessageModel> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface MessageClickAdapter {
        void onClick(String id, TextView title, TextView contents, TextView date);

    }

    public void setOnMessageClickListener(MessageClickAdapter messageClickAdapter) {
        this.messageClickAdapter = messageClickAdapter;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_msg, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (null != list && list.size() > 0) {
            final MessageModel messageModel = list.get(position);
            holder.msgTitle.setText(messageModel.title);
            holder.msgDate.setText(messageModel.create_time);
            holder.msgContent.setText(messageModel.content);

            if (messageModel.is_read == 0) {
                holder.msgTitle.setTextColor(context.getResources().getColor(R.color.black_0d0c0c));
                holder.msgContent.setTextColor(context.getResources().getColor(R.color.black_0d0c0c));
                holder.msgDate.setTextColor(context.getResources().getColor(R.color.black_0d0c0c));
            } else {
                holder.msgTitle.setTextColor(context.getResources().getColor(R.color.nav_color_txt));
                holder.msgContent.setTextColor(context.getResources().getColor(R.color.nav_color_txt));
                holder.msgContent.setTextColor(context.getResources().getColor(R.color.nav_color_txt));
            }

            holder.clickMsg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (messageClickAdapter != null) {
                        messageClickAdapter.onClick(messageModel.message_id, holder.msgTitle, holder.msgContent, holder.msgDate);
                    }
                }
            });

            final ViewHolder finalHolder = holder;
            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!NoDoubleClickUtils.isDoubleClick()) {
                        finalHolder.msgSwipe.smoothClose();
                        if (msgDeleteClick != null) {
                            msgDeleteClick.MessageDeleteClick(position,messageModel.message_id);
                        }
                    }
                }
            });



        }
        return convertView;
    }


    class ViewHolder {
        @BindView(R.id.msg_type_icon)
        ImageView msgTypeIcon;
        @BindView(R.id.msg_title)
        TextView msgTitle;
        @BindView(R.id.msg_date)
        TextView msgDate;
        @BindView(R.id.msg_content)
        TextView msgContent;
        @BindView(R.id.click_message_layout)
        LinearLayout clickMsg;
        @BindView(R.id.delete)
        TextView delete;
        @BindView(R.id.msg_swipe)
        SwipeMenuLayout msgSwipe;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
